"use strict";
const mvc = require('cosjs.mvc');
const debug = mvc.library.require('debug');

exports.add = function (id) {
    if (!arguments[1]) {
        return;
    }
    let config = mvc.config('hero', id);
    if (!config) {
        return debug('config not exist', 'hero', id);
    }
    // let itype = this.iTypes(id);
    // //不可以堆叠，装备模式
    // if(!itype['stack']){
    //     return this.item.insert(id,arguments[1]);
    // }
    //碎片模式
    if (!this._insert_hero_soul) {
        this._insert_hero_soul = {};
        this.once('verify', verify_hero_soul.bind(this));
    }
    if (!this._insert_hero_soul[id]) {
        this._insert_hero_soul[id] = parseInt(arguments[1]);
    }
    else {
        this._insert_hero_soul[id] += parseInt(arguments[1]);
    }
    this.key(id);
    this.key(config["soul"]);
    this.record.key(1300);
    this.record.key(1500);
    this.record.key(1600);
    this.record.key(1700);
}

exports.format = function (item, act) {
    mvc.format(["item", "hero"], item, true);
    //初始技能
    let config = mvc.config('hero', item["id"]);
    item.skills[0] = config.skill[0];
    //let attach = mvc.library('arrmap/attach',"hero",item["attach"]);
    //attach["slv"] = config["star"];
    return item;
}



function verify_hero_soul() {
    if (!this._insert_hero_soul) {
        return
    }
    for (let k in this._insert_hero_soul) {
        let ty = this.item.types(parseInt(k));
        let hd = this.item.get(ty);
        if (!hd) {
            this.item.act('add', ty['id'], 'val', 1, ty['bag']);
            this._insert_hero_soul[k] -= 1;
            //成就，武将收集数量的记录点
            this.record.add(1300, 1);
            let config = mvc.config("hero", k);
            switch (config.qlv) {
                case 2:
                    this.record.add(1500, 1);
                    break;
                case 3:
                    this.record.add(1600, 1);
                    break;
                case 4:
                    this.record.add(1700, 1);
                    break;
            }

        }
        if (this._insert_hero_soul[k] > 0) {
            let config = mvc.config("hero", k);
            if (config && config["soul"]) {
                this.add(config["soul"], this._insert_hero_soul[k] * config["resolve"]);
            }
        }
    }
    this._insert_hero_soul = null;

}